package cn.edu.scau.cmi.tangqingning.listener;

import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 统计在线人数
 *@author 唐庆宁
 */
@WebListener
public class CountLineListener implements HttpSessionListener {

	/**
	 * 创建session时调用
	 * 
	 * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
	 */
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub

	}

	/**
	 * 销毁session时调用
	 * 
	 * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		ServletContext context = arg0.getSession().getServletContext();
		Integer count = (Integer) context.getAttribute("count");
		if (count != null && count.intValue() >= 1) {
			int co = count.intValue();
			count = new Integer(co - 1);
			context.setAttribute("count", count);
			System.out.println("<<<<_____销毁session......");
		} else {
			count = new Integer(0);
		}
		System.out.println("当前用户人数: " + count + "    " + new Date());
	}

}
